package net.pimpas.web.jsf.resourcemanager;

import java.io.IOException;

import javax.faces.context.FacesContext;

import net.pimpas.logging.serviceprovider.LoggingServiceProvider;
import net.pimpas.logging.serviceprovider.LoggingServiceProviderFactoryImpl;

import org.apache.myfaces.renderkit.html.util.AddResource;
import org.apache.myfaces.renderkit.html.util.AddResourceFactory;

public class ResourceManager {
	
	private static final LoggingServiceProvider logger = LoggingServiceProviderFactoryImpl.getLoggingServiceProvider(ResourceManager.class);
	private AddResource addResource;
	private FacesContext context;
	private Class component;
	private ResourceHandler handler;
	
	public ResourceManager(FacesContext context, Class component) {
		this.setContext(context);
		this.setComponent(component);
		this.setAddResource(AddResourceFactory.getInstance(this.getContext()));
		this.setHandler(new ResourceHandler(this.getComponent()));
	}
	
	private void logAddedResource(String resource) {
		logger.debug("[RESOURCE MANAGER]: Added Resource ("+resource+") for component "+
				this.getComponent().getSimpleName()+".");
	}
	
	public void addJavascriptHeader(String resource) {
		this.getHandler().configure(resource);
		this.getAddResource().addJavaScriptAtPosition(this.getContext(),
				AddResource.HEADER_BEGIN,
				this.getHandler());
		this.logAddedResource(resource);
	}
	
	public void addCustomJavascriptHeader(String resource, Class component ) {
		// save
		Class defaultComponent = this.getComponent();
		this.setComponent(component);
		this.getHandler().setComponent(this.getComponent());
		
		this.getHandler().configure(resource);
		this.getAddResource().addJavaScriptAtPosition(this.getContext(),
				AddResource.HEADER_BEGIN,
				this.getHandler());
		this.logAddedResource(resource);
		
		// restore
		this.setComponent(defaultComponent);
		this.getHandler().setComponent(this.getComponent());
	}
	
	public void addJavascriptHere(String resource) {
		this.getHandler().configure(resource);
		try {
			this.getAddResource().addJavaScriptHere(this.getContext(),
					this.getHandler());
			this.logAddedResource(resource);
		} catch (IOException e) {
			logger.error("[RESOURCE MANAGER]: Error when trying to add resource ("+resource+")", e);
		}
	}
	
	public void addStylesheetHeader(String resource) {
		this.getHandler().configure(resource);
		this.getAddResource().addStyleSheet(this.getContext(),
				AddResource.HEADER_BEGIN,
				this.getHandler());
		this.logAddedResource(resource);
	}
	
	public void addStylesheetHere(String uri) {
		this.getAddResource().addStyleSheet(this.getContext(),
				AddResource.HEADER_BEGIN,
				uri);
		this.logAddedResource(uri);
	}
	
	public String getResourceUri(String resource) {
		this.getHandler().configure(resource);
		return this.getAddResource().getResourceUri(this.getContext(),
				this.getHandler());
	}
	
	public AddResource getAddResource() {
		return addResource;
	}

	public void setAddResource(AddResource addResource) {
		this.addResource = addResource;
	}

	public FacesContext getContext() {
		return context;
	}

	public void setContext(FacesContext context) {
		this.context = context;
	}

	public Class getComponent() {
		return component;
	}

	public void setComponent(Class component) {
		this.component = component;
	}

	public ResourceHandler getHandler() {
		return handler;
	}

	public void setHandler(ResourceHandler handler) {
		this.handler = handler;
	}
}